package com.example.tenant.controller;


import com.example.tenant.config.DataSourceContextHolder;
import com.example.tenant.entity.User;
import com.example.tenant.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Welisit
 * @since 2020-06-24
 */
@RestController
@RequestMapping("/tenant/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 测试数据源切换
     * @param tenantKey
     * @return
     */
    @GetMapping
    public User getUser(@RequestParam("tenantkey") String tenantKey) {
        log.info("更新租户id值为"+tenantKey);
        DataSourceContextHolder.setDBType(tenantKey);
        return userService.getById(1);
    }

    /**
     * 测试事务
     * @param tenantKey
     * @param username
     * @return
     */
    @GetMapping("update")
    public Boolean getUser(@RequestParam("tenantkey") String tenantKey, @RequestParam("username") String username) {
        log.info("更新租户id值为"+tenantKey);
        DataSourceContextHolder.setDBType(tenantKey);
        userService.updateUserName("1", username);
        return true;
    }


}

